package design.simdi.code.final_paper.site.panels.proiecte.bibliografie;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator;

import design.simdi.code.final_paper.core.entities.Bibliography;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.core.services.ApplicationService;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.core.services.UserService;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.MyWizard;
import design.simdi.code.final_paper.site.panels.MyWizardButtonBar;

/**
 * 
 * @author Marius Dumitriu - (Jun 21, 2010 - 4:12:59 PM)
 * 
 */
public class AddBibliographyWizardPanel extends MyWizard {

	private static final long serialVersionUID = 1L;

	private Bibliography bibliography;

	private TextField<String> name;
	private TextArea<String> description;
	private TextField<String> link;
	private String title;
	private String subtitle;
	@SpringBean
	private ApplicationService applicationService;

	@SpringBean
	private UserService userService;

	public AddBibliographyWizardPanel(String id, Bibliography bibliography) {
		super(id, false);

		if (bibliography == null) {
			bibliography = new Bibliography();
			title = "form.add.title";
			subtitle = "form.add.subtitle";
		} else {
			title = "form.modify.title";
			subtitle = "form.modify.subtitle";
		}
		this.bibliography = bibliography;

		setDefaultModel(new CompoundPropertyModel<AddBibliographyWizardPanel>(this));
		WizardModel model = new WizardModel();
		model.add(new BibliographyNameStep());

		init(model);
		((WicketApplication) getApplication()).setShowErrors(false);
	}

	protected Component newButtonBar(String id) {
		return new MyWizardButtonBar(id, this, getForm()) {

			private static final long serialVersionUID = 1L;

			public void onFinishClick(AjaxRequestTarget target) {

				try {

					onFinish(target);
					BibliografieTabPanel panel = new BibliografieTabPanel("panel");
					panel.setOutputMarkupId(true);

					Page page = this.getPage();

					page.get("content:tabs:panel").replaceWith(panel);

					target.addComponent(page.get("content"));

				} catch (ServiceException e) {

					error(e.getMessage());
				}

			}

			public void onCancelClick(AjaxRequestTarget target) {
				onCancel(target);
				BibliografieTabPanel panel = new BibliografieTabPanel("panel");
				panel.setOutputMarkupId(true);

				Page page = this.getPage();

				page.get("content:tabs:panel").replaceWith(panel);

				target.addComponent(page.get("content"));
			}

		};
	}

	public void onFinish(AjaxRequestTarget target) throws ServiceException {

		if (bibliography.getPerson() == null) {
			User currentUser = ((MyAuthenticatedWebSession) getSession()).getCurrentUser();

			Person p = userService.findPersonByUser(currentUser);

			bibliography.setPerson(p);
		}

		applicationService.saveBibliography(bibliography);
		onFinishMessage(target);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	private final class BibliographyNameStep extends WizardStep {

		private static final long serialVersionUID = 1L;

		public BibliographyNameStep() {
			super(new ResourceModel(title), new ResourceModel(subtitle));

			add(name = new TextField<String>("name", new PropertyModel<String>(bibliography, "name")));
			add(description = new TextArea<String>("description",
					new PropertyModel<String>(bibliography, "description")));
			add(link = new TextField<String>("link", new PropertyModel<String>(bibliography, "link")));
			name.setRequired(true);
			description.setRequired(false);
			link.add(new AbstractValidator<String>() {

				private static final long serialVersionUID = 1L;

				@Override
				protected void onValidate(IValidatable<String> validatable) {
					String value = validatable.getValue();
					if (value.length() != 0) {
						if (!(value.startsWith("http://") || value.startsWith("https://")))
							error(validatable, "error.proiecte.bibliografie.link");
						if (value.length() <= 13)
							error(validatable, "error.proiecte.bibliografie.link.scurt");
					}
				}
			});
			link.setRequired(false);
			name.setType(String.class);
			description.setType(String.class);
			link.setType(String.class);

		}

	}

}
